#!/bin/bash
#
# Build a Fedora Workstation Live ISO by livemedia-creator. The Live iso will be stored in the `/images/` directory.
# We have to build the RPMs files of Anaconda first and then add them as volume
# mount to /anaconda-rpms to the container (could be RO mount).
#
#   sudo make -f ./Makefile.am container-rpms-scratch
#   sudo podman run -i --rm --privileged --tmpfs /var/tmp:rw,mode=1777 -v `pwd`/result/build/01-rpm-build:/anaconda-rpms:ro -v `pwd`/output-dir:/images:z quay.io/rhinstaller/anaconda-live-iso-creator:main
#
# Input directory:
# /anaconda-rpms/ (Anaconda RPM files for the build)
#
# Output directory:
# /images (Where the boot.iso will be stored)
{% if distro_name == "rhel" %}
#
# WARNING: this solution is not supported on RHEL/CentOS Stream
#
{% endif %}

set -eux

INPUT_RPMS=/anaconda-rpms/
REPO_DIR=/tmp/anaconda-rpms/
OUT_DIR=/images/
LOG_DIR="$OUT_DIR"/logs/
# fedora-kickstarts clone during the Anaconda build
# https://pagure.io/fedora-kickstarts/tree/main
WORKSTATION_KS=/lorax/workstation.ks

HTTP_LOG=$LOG_DIR/http.log
HTTP_PID=/lorax/httpd.pid

start_http_server() {
    python3 -m http.server -d . 1>&2 2>$HTTP_LOG &
    echo "$!" > $HTTP_PID

    # extract container IP
    IP=$(ip -4 addr show scope global | grep -oP 'inet \K[\d.]+' | head -n1)
    echo "http://$IP:8000/"
}

stop_http_server() {
    kill -s SIGTERM "$(cat $HTTP_PID)"
}

# pre-create loop devices manually. In the container you can't use losetup for that.
mknod -m 0660 /dev/loop0 b 7 0  2> /dev/null || true
mknod -m 0660 /dev/loop1 b 7 1  2> /dev/null || true
mknod -m 0660 /dev/loop2 b 7 2  2> /dev/null || true
mknod -m 0660 /dev/loop3 b 7 3  2> /dev/null || true


#### create and serve repo from provided Anaconda RPMs ####
mkdir -p $REPO_DIR
cp -a $INPUT_RPMS/* $REPO_DIR || echo "RPM files can't be copied!"  # We could just do the build with official repositories only
createrepo_c $REPO_DIR
pushd $REPO_DIR
HTTP_SERVER=$(start_http_server)
popd


#### get ISO for build ####
# download new cockpit CI iso (it's gated)
mkdir /var/tmp/lmc-build
pushd /var/tmp/lmc-build
git clone --depth 1 https://github.com/cockpit-project/bots.git
{% if distro_release == "rawhide" %}
./bots/image-download fedora-rawhide-boot
cp -L ./bots/images/fedora-rawhide-boot /lorax/fedora-boot.iso
{% else %}
./bots/image-download fedora-{$ distro_release $}-boot
cp -L ./bots/images/fedora-{$ distro_release $}-boot /lorax/fedora-boot.iso
{% endif %}
rm -rf ./bots
popd


#### find out recommended power for qemu ####
mem=$(awk -F":" '$1~/MemTotal/{print int($2 / 1024) }' /proc/meminfo )
cpu=$(nproc)

# use 80% of available power
proposed_cpu=$((cpu*80/100))
proposed_mem=$((mem*80/100))

# do not get lower than 2048MB (default) and 1 cpu
if [ $proposed_mem -lt 2048 ]; then
    proposed_mem=2048
fi

if [ $proposed_cpu -lt 1 ]; then
    proposed_cpu=1
fi

echo "Virtual machine will start with cpu:$proposed_cpu memory:${proposed_mem}MB"


#### add webui dependencies to kickstart ####
cat >> $WORKSTATION_KS << EOF
repo --name anaconda-local --baseurl=$HTTP_SERVER

%packages
anaconda-webui
%end
EOF

cp $WORKSTATION_KS /images/logs/input.ks

#### build the live ISO ####
livemedia-creator \
    --make-iso \
    --iso-only \
    --iso-name="Fedora-Workstation.iso" \
    --macboot \
    --project=Fedora-Workstation-Live \
    --releasever={$ distro_release $} \
    --vcpus=$proposed_cpu \
    --ram=$proposed_mem \
    --iso=/lorax/fedora-boot.iso \
    --ks=$WORKSTATION_KS \
    --logfile="$LOG_DIR/build.log" \
    --resultdir=/lorax/result

stop_http_server

cp ./result/Fedora-Workstation.iso "$OUT_DIR"

# fix permissions to user permissions on the built artifacts
chown -Rv --reference="$INPUT_RPMS" "$OUT_DIR"
